//
//  ViewController.m
//  CloseLIght
//
//  Created by dllo on 15/10/30.
//  Copyright (c) 2015年 WLM. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@property (nonatomic, retain) UIButton *button;
@property (nonatomic, retain) NSMutableArray *data;




@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.data = [[NSMutableArray alloc] init];
    for (int i = 0; i < 10; i++) {
        for (int j = 0; j < 8; j++) {
            self.button = [UIButton buttonWithType:UIButtonTypeSystem];
            self.button.frame = CGRectMake(30 + 40 * j, 50 + 40 * i, 35, 35);
            self.button.backgroundColor = [UIColor grayColor];
            [self.view addSubview:self.button];
            [self.button addTarget:self action:@selector(change:) forControlEvents:UIControlEventTouchUpInside];
            [_data addObject:self.button];
        }
    }
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(100, 500, 200, 40);
    button.center = CGPointMake(self.view.center.x, 500);
    [button setTitle:@"重新游戏" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(buttonDidPress:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    
    
}

- (void)buttonDidPress:(UIButton *)sender {
    for (UIButton *button in _data) {
        button.backgroundColor = [UIColor grayColor];
    }
    
}

- (void)change:(UIButton *)sender {
    if (sender.backgroundColor == [UIColor orangeColor]) {
        sender.backgroundColor = [UIColor grayColor];
    }else {
        sender.backgroundColor = [UIColor orangeColor];
    }
    for (UIButton *button in _data) {
        if (fabs(button.center.x - sender.center.x) + fabs(button.center.y - sender.center.y) == 40) {
            if (button.backgroundColor == [UIColor grayColor]) {
                button.backgroundColor = [UIColor orangeColor];
            } else {
                button.backgroundColor = [UIColor grayColor];
            }

        }
    }
    
}







- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
